package com.jie.config;

import com.jie.enums.ArticleFlowEvent;
import com.jie.enums.ArticleFlowStateEnum;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigBuilder;
import org.springframework.statemachine.config.builders.StateMachineModelConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.stereotype.Component;

import java.util.EnumSet;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/30 13:45
 **/
@Component
@EnableStateMachine
public class ArticleStateMachine extends StateMachineConfigurerAdapter<ArticleFlowStateEnum, ArticleFlowEvent> {


    /**
     * 配置状态
     * @param states
     * @throws Exception
     */
    public void configure(StateMachineStateConfigurer<ArticleFlowStateEnum, ArticleFlowEvent> states) throws Exception{
        states.withStates()
                .initial(ArticleFlowStateEnum.CREATED)
                .states(EnumSet.allOf(ArticleFlowStateEnum.class));

    }

    /**
     * 配置状态转换事件关系
     * @param transition
     * @throws Exception
     */
    public void configure(StateMachineTransitionConfigurer<ArticleFlowStateEnum, ArticleFlowEvent> transition) throws Exception{
        transition.
                //脱敏事件：文章->脱敏
                withExternal().source(ArticleFlowStateEnum.CREATED).target(ArticleFlowStateEnum.DESENSITIZATION).event(ArticleFlowEvent.REPLACE)
                .and()
                //审核事件：脱敏->审核
                .withExternal().source(ArticleFlowStateEnum.DESENSITIZATION).target(ArticleFlowStateEnum.COMPLETED).event(ArticleFlowEvent.AUDIT)
                .and() //审核：审核->审核失败
                .withExternal().source(ArticleFlowStateEnum.COMPLETED).target(ArticleFlowStateEnum.DESENSITIZATION).event(ArticleFlowEvent.AUDIT_FAIL)
                .and() //审核：审核->审核成功
                .withExternal().source(ArticleFlowStateEnum.COMPLETED).target(ArticleFlowStateEnum.COMPLETED_SUCCESS).event(ArticleFlowEvent.AUDIT_SUCCESS)
                .and() //发布时间：审核成功->发布
                .withExternal().source(ArticleFlowStateEnum.COMPLETED_SUCCESS).target(ArticleFlowStateEnum.PUBLISHED).event(ArticleFlowEvent.PUBLISH);
    }

}
